#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_WriteDataById_0x2E_test.py
@Time    :   2022/9/13 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from gettext import find
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SID_0x19_reportDTCByStatusMask_noFault = [('19 02 01', '59 02 FF'),
                                          ('19 02 02', '59 02 FF'),
                                          ('19 02 04', '59 02 FF'),
                                          ('19 02 08', '59 02 FF'),
                                          ('19 02 09', '59 02 FF'),
                                          ('19 02 10', '59 02 FF'),
                                          ('19 02 0A', '59 02 FF'),
                                          ('19 02 0B', '59 02 FF'),
                                          ('19 02 0F', '59 02 FF'),
                                          ('19 02 20', '59 02 FF'),
                                          ('19 02 40', '59 02 FF'),
                                          ('19 02 80', '59 02 FF'),
                                          ('19 02 7F', '59 02 FF'),
                                          ('19 02 FF', '59 02 FF'),
                                       ]

class Test_reportDTCByStatusMask_Phy(object):

   '''NO Fault'''
   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_Default_Phy(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_extended_Phy(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)


   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   # @pytest.mark.repeat(10)
   # def test_reportDTCByStatusMask_No_Fault_Restart_withPowerOff_Phy(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 1, iStep, False, 'UDS', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
   #    iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
   #    time.sleep(2)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 0, iStep, False, 'UDS', 'noClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.repeat(10)
   def test_reportDTCByStatusMask_No_Fault_Restart_with11_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
      iStep = Driver['CAN'].restartMRR_11(Driver,iStep)
      time.sleep(2)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 0, iStep, False, 'UDS', 'noClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   
   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   # def test_DTC_VBAT_HIGH_ByStatusMask_defaul_Phy(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(0.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 1, iStep, False, 'UDS', 'ClearFault')

   #    # 设置电压为17V，并等待1.5s 制造 Overvoltage 故障，读取当前故障
   #    iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 17 2F', 0, iStep, False, 'UDS', 'noClearFault')

   #    # 设置电压为12V，并等待1.5s 恢复 Overvoltage 故障，读取当前故障
   #    iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 17 2E', 0, iStep, False, 'UDS', 'noClearFault')

   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
   # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   # def test_DTC_VBAT_LOW_ByStatusMask_extended_Phy(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(0.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 3, iStep, False, 'UDS', 'ClearFault')

   #    # 设置电压为8V，并等待1.5s 制造 Undervoltage 故障，读取当前故障 
   #    iStep = Driver['CAN'].SetVolt(Driver, 8, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 16 2F', 0, iStep, False, 'UDS', 'noClearFault')

   #    # 设置电压为12V，并等待1.5s 恢复 Undervoltage 故障，读取当前故障
   #    iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 16 2E', 0, iStep, False, 'UDS', 'noClearFault')
   
   
   
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   def test_DTC_DLostCommunication_ByStatusMask_defaul_Phy(self, action,  Driver):
      iStep = 1
      time.sleep(1.5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

      # 暂停发送FZCU_BodyCAN_0x90信号，制造Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF D0 97 87 2F', 0, iStep, False, 'UDS', 'noClearFault')

      # 恢复发送FZCU_BodyCAN_0x90信号，恢复Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 FF D0 97 87 2E', 0, iStep, False, 'UDS', 'noClearFault')
      time.sleep(5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 FF', 0, iStep, False, 'UDS', 'noClearFault')
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
      time.sleep(2)


   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   def test_DTC_DLostCommunication_ByStatusMask_Extended_Phy(self, action,  Driver):
      iStep = 1
      time.sleep(1.5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 3, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

      # 暂停发送FZCU_BodyCAN_0x90信号，制造Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF D0 97 87 2F', 0, iStep, False, 'UDS', 'noClearFault')

      # 恢复发送FZCU_BodyCAN_0x90信号，恢复Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 FF D0 97 87 2E', 0, iStep, False, 'UDS', 'noClearFault')
      logging.info('wait 5s')
      time.sleep(5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 FF', 0, iStep, False, 'UDS', 'noClearFault')
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
      time.sleep(2)


class Test_reportDTCByStatusMask_Fun(object):

   '''NO Fault'''
   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_Default_Fun(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   ids = ["Request {0}".format(req, resp) for req, resp in SID_0x19_reportDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with functional addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('req,resp', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_extended_Fun(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)


   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   # @pytest.mark.repeat(10)
   # def test_reportDTCByStatusMask_No_Fault_Restart_withPowerOff_Fun(self, action,  Driver):
   #    iStep = 1
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 1, iStep, False, 'UDSFun', 'ClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
   #    iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
   #    time.sleep(2)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 0, iStep, False, 'UDSFun', 'noClearFault')
   #    iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.repeat(10)
   def test_reportDTCByStatusMask_No_Fault_Restart_with11_Fun(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 1, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
      iStep = Driver['CAN'].restartMRR_11(Driver,iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 01", '59 02 FF', 0, iStep, False, 'UDSFun', 'noClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   
   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   # def test_DTC_VBAT_HIGH_ByStatusMask_defaul_Fun(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(0.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 1, iStep, False, 'UDSFun', 'ClearFault')

   #    # 设置电压为17V，并等待1.5s 制造 Overvoltage 故障，读取当前故障
   #    iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 17 2F', 0, iStep, False, 'UDSFun', 'noClearFault')

   #    # 设置电压为12V，并等待1.5s 恢复 Overvoltage 故障，读取当前故障
   #    iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 17 2E', 0, iStep, False, 'UDSFun', 'noClearFault')

   # @allure.severity('normal')
   # @allure.story('Test read dtc information service 0x19')
   # @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with functional addressing')
   # @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   # def test_DTC_VBAT_LOW_ByStatusMask_extended_Fun(self, action,  Driver):
   #    iStep = 1
   #    time.sleep(0.5)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 3, iStep, False, 'UDSFun', 'ClearFault')

   #    # 设置电压为8V，并等待1.5s 制造 Undervoltage 故障，读取当前故障 
   #    iStep = Driver['CAN'].SetVolt(Driver, 8, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 16 2F', 0, iStep, False, 'UDSFun', 'noClearFault')

   #    # 设置电压为12V，并等待1.5s 恢复 Undervoltage 故障，读取当前故障
   #    iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
   #    iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF F0 03 16 2E', 0, iStep, False, 'UDSFun', 'noClearFault')
   
   
   
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   def test_DTC_DLostCommunication_ByStatusMask_defaul_Fun(self, action,  Driver):
      iStep = 1
      time.sleep(1.5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 1, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

      # 暂停发送FZCU_BodyCAN_0x90信号，制造Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF D0 97 87 2F', 0, iStep, False, 'UDSFun', 'noClearFault')

      # 恢复发送FZCU_BodyCAN_0x90信号，恢复Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 FF D0 97 87 2E', 0, iStep, False, 'UDSFun', 'noClearFault')
      time.sleep(5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 FF', 0, iStep, False, 'UDSFun', 'noClearFault')
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
      time.sleep(2)


   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with functional addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   def test_DTC_DLostCommunication_ByStatusMask_Extended_Fun(self, action,  Driver):
      iStep = 1
      time.sleep(1.5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF', 3, iStep, False, 'UDSFun', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

      # 暂停发送FZCU_BodyCAN_0x90信号，制造Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 FF D0 97 87 2F', 0, iStep, False, 'UDSFun', 'noClearFault')

      # 恢复发送FZCU_BodyCAN_0x90信号，恢复Lost Communication故障
      iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
      time.sleep(1)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 08', '59 02 FF D0 97 87 2E', 0, iStep, False, 'UDSFun', 'noClearFault')
      logging.info('wait 5s')
      time.sleep(5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDSFun', 'noClearFault')
      time.sleep(2)
